# 显式导入所有模型模块，添加新的模型类所在模块（users模块）的导入
from mooc.models.admin import *  # noqa
from mooc.models.account import *  # noqa
from mooc.models.uni_account import *  # noqa
from mooc.models.core import *  # noqa  
from mooc.models.userapi import *  # noqa  
from mooc.models.users import *  # noqa  # 新增导入，确保能识别users.py里的模型类所在模块

# 导出常用的模型类
from mooc.models.account import (
    Account,
    AccountWechats,
    AccountAliapp,
    AccountBaiduapp,
    AccountPhoneapp,
    AccountToutiaoapp,
    AccountWebapp,
    AccountWxapp,
    AccountXzapp
)

from mooc.models.uni_account import (
    UniAccount,
    UniAccountExtraModules,
    UniAccountGroup,
    UniAccountMenus,
    UniAccountModules,
    UniAccountModulesShortcut,
    UniAccountUsers,
    UniGroup,
    UniLinkUniacid,
    UniModules,
    UniSettings,
    UniVerifycode
)

from mooc.models.core import (
    CoreAttachment,
    CoreCache,
    CoreCron,
    CoreCronRecord,
    CoreJob,
    CoreMenu,
    CoreMenuShortcut,
    CorePaylog,
    CorePerformance,
    CoreQueue,
    CoreRefundlog,
    CoreResource,
    CoreSendsmsLog,
    CoreSessions,
    CoreSettings
)

from mooc.models.userapi import (
    UserapiCache,
    UserapiReply
)

from mooc.models.users import (
    Users,
    UsersBind,
    UsersCreateGroup,
    UsersGroup,
    UsersInvitation,
    UsersLastUse,
    UsersLoginLogs,
    UsersOperateHistory,
    UsersOperateStar,
    UsersPermission,
    UsersProfile,
    UsersExtraGroup,
    UsersExtraLimit,
    UsersExtraModules,
    UsersExtraTemplates,
    UsersFailedLogin,
    UsersFounderGroup,
    UsersFounderOwnCreateGroups,
    UsersFounderOwnUniGroups,
    UsersFounderOwnUsers,
    UsersFounderOwnUsersGroups
)

from mooc.models.mc import(
    McCreditsRecharge,
    McCashRecord,
    McChatsRecord,
    McMappingFans,
    McMassRecord,
    McCreditsRecord,
    MCFansGroups,
    McMembers,
    MCFansTag,
    MCFansTagMapping,
    MCGroups,
    MCHandsel,
    McMemberAddress,
    McMemberFields,
    McMemberProperty,
    McOauthFans
)

from mooc.models.modules import(
    IMSModulesBindings,
    IMSModulesCloud,
    Modules,
    IMSModulesIgnore,
    IMSModulesPlugin,
    IMSModulesPluginRank,
    IMSModulesRank,
    IMSModulesRecycle,
)

def verify_all_models():
    """验证所有模型是否已正确注册到Base.metadata"""
    from mooc.db.database import Base
    
    # 所有预期的表名，添加新模型对应的表名
    expected_tables = {
        'ims_goouc_fullexam_admin',
        # Account 相关表
        'ims_account',
        'ims_account_aliapp',
        'ims_account_baiduapp',
        'ims_account_phoneapp',
        'ims_account_toutiaoapp',
        'ims_account_webapp',
        'ims_account_wechats',
        'ims_account_wxapp',
        'ims_account_xzapp',
        
        # UniAccount 相关表
        'ims_uni_account',
        'ims_uni_account_extra_modules',
        'ims_uni_account_group',
        'ims_uni_account_menus',
        'ims_uni_account_modules',
        'ims_uni_account_modules_shortcut',
        'ims_uni_account_users',
        'ims_uni_group',
        'ims_uni_link_uniacid',
        'ims_uni_modules',
        'ims_uni_settings',
        'ims_uni_verifycode',
        
        # Core 相关表名
        "ims_core_attachment",
        "ims_core_cache",
        "ims_core_cron",
        "ims_core_cron_record",
        "ims_core_job",
        "ims_core_menu",
        "ims_core_menu_shortcut",
        "ims_core_paylog",
        "ims_core_performance",
        "ims_core_queue",
        "ims_core_refundlog",
        "ims_core_resource",
        "ims_core_sendsms_log",
        "ims_core_sessions",
        "ims_core_settings",
        
        # Userapi 相关表名
        "ims_userapi_cache",
        "ims_userapi_reply",
        
        # Users 相关表名
        "ims_users",
        "ims_users_bind",
        "ims_users_create_group",
        "ims_users_group",
        "ims_users_invitation",
        "ims_users_lastuse",
        "ims_users_login_logs",
        "ims_users_operate_history",
        "ims_users_operate_star",
        "ims_users_permission",
        "ims_users_profile",
        "ims_users_extra_group",
        "ims_users_extra_limit",
        "ims_users_extra_modules",
        "ims_users_extra_templates",
        "ims_users_failed_login",
        "ims_users_founder_group",
        "ims_users_founder_own_create_groups",
        "ims_users_founder_own_uni_groups",
        "ims_users_founder_own_users",
        "ims_users_founder_own_users_groups",
        # part 3
        "ims_goouc_fullexam_advert",
        "ims_goouc_fullexam_banji",
        "ims_goouc_fullexam_banner",
        "ims_goouc_fullexam_category",
        "ims_goouc_fullexam_cdkey",
        "ims_goouc_fullexam_cdkeys",
        "ims_goouc_fullexam_cdkey_cate",
        "ims_goouc_fullexam_exercise",
        "ims_goouc_fullexam_feedback",
        "ims_goouc_fullexam_gift",
        "ims_goouc_fullexam_index_btn",
        "ims_goouc_fullexam_knowledge",
        "ims_goouc_fullexam_knowledge_cate",
        "ims_goouc_fullexam_notice",
        "ims_goouc_fullexam_order",
        "ims_goouc_fullexam_paper",
        "ims_goouc_fullexam_paper_test",
        "ims_goouc_fullexam_phonecode",
        "ims_goouc_fullexam_q_year",
        "ims_goouc_fullexam_school",
        "ims_goouc_fullexam_setting",
        "ims_goouc_fullexam_share_record",
        "ims_goouc_fullexam_son_simple",
        "ims_goouc_fullexam_test",
        "ims_goouc_fullexam_test_type",
        "ims_goouc_fullexam_type_cate",
        "ims_goouc_fullexam_watermark",
        "ims_goouc_fullexam_wxtpl",
        "ims_goouc_fullexam_xuesheng",
        "ims_goouc_fullexam_user",
        "ims_goouc_fullexam_user_collection_praction",
        "ims_goouc_fullexam_user_doexam",
        "ims_goouc_fullexam_user_doother_exam",
        "ims_goouc_fullexam_user_doother_exam_answer",
        "ims_goouc_fullexam_user_exam_answer",
        "ims_goouc_fullexam_user_formid",
        "ims_goouc_fullexam_user_gift",
        "ims_goouc_fullexam_user_knowledge_cate",
        "ims_goouc_fullexam_user_member",
        "ims_goouc_fullexam_user_pool",
        "ims_goouc_fullexam_user_qhigh",
        "ims_goouc_fullexam_user_qintensive",
        "ims_goouc_fullexam_user_qtype",
        "ims_goouc_fullexam_user_read",
        "ims_goouc_fullexam_user_special",
        "ims_goouc_fullexam_user_spequence",
        "ims_goouc_fullexam_user_wrong_praction",
        #part 4
        "ims_profile_fields",
        "ims_mobilenumber",
        "ims_modules_bindings",
        "ims_modules_cloud",
        "ims_modules",
        "ims_modules_ignore",
        "ims_modules_plugin",
        "ims_modules_plugin_rank",
        "ims_modules_rank",
        "ims_modules_recycle",

        "ims_mc_credits_recharge",
        "ims_mc_cash_record",
        "ims_mc_chats_record",
        "ims_mc_mapping_fans",
        "ims_mc_mass_record",
        "ims_mc_credits_record",
        "ims_mc_fans_groups",
        "ims_mc_members",
        "ims_mc_fans_tag",
        "ims_mc_fans_tag_mapping",
        "ims_mc_groups",
        "ims_mc_handsel",
        "ims_mc_member_address",
        "ims_mc_member_fields",
        "ims_mc_member_property",
        "ims_mc_oauth_fans",

    }
    
    actual_tables = set(Base.metadata.tables.keys())
    missing_tables = expected_tables - actual_tables
    extra_tables = actual_tables - expected_tables
    
    if missing_tables:
        raise RuntimeError(f"Missing tables in metadata: {missing_tables}")
    
    if extra_tables:
        print(f"Warning: Found unexpected tables: {extra_tables}")
    
    return True

def get_all_table_names():
    """获取所有已注册的表名"""
    from mooc.db.database import Base
    return sorted(Base.metadata.tables.keys())